package com.aaa.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/7/26 9:20
 * @description： 测试jdbc
 * @modified By：
 * @version:
 */
public class JdbcTest {
    /**
     * 四大金刚
     */
    static  String driver="com.mysql.jdbc.Driver";
    static  String  url="jdbc:mysql://localhost:3306/qy156?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";
    static  String username="root";
    static  String password="root";
    /**
     * 1、加载驱动程序
     */
    static{
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description:2、获取数据库连接
     * @create time: 2022/7/26 9:23
     * @param
     * @return java.sql.Connection
     */
    public static Connection getConnection(){
        /**
         *      * @param url a database url of the form
         *      * <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
         *      * @param user the database user on whose behalf the connection is being
         *      *   made
         *      * @param password the user's password
         */
        Connection connection=null;
        try {
             connection = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  connection;
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 通用的增删改方法
     * @create time: 2022/7/26 10:31
     * @param sql
     * @param params  可变参数，0或者N个
     * @return boolean
     */
    public static boolean executeUpdate(String sql,Object...params){
        //测试获取数据库连接
        Connection connection = JdbcTest.getConnection();
        PreparedStatement ps=null;
        //3、编写sql语句
        // String sql="insert into dept (dname,loc) values('邮电部1','北京')";
        /*String sql="delete  from dept where deptno>50";*/
        try {
            //4、获取PreparedStatement
            ps = connection.prepareStatement(sql);
            //替换占位符
           /* preparedStatement.setString(1,params[0].toString());
            preparedStatement.setString(2,params[1].toString());
            preparedStatement.setString(3,params[2].toString());*/
           if(null!=params&&params.length>0){
               for (int i = 0; i < params.length; i++) {
                   //i+1是占位符的下标，从1开始，params[i] 是传入的第几个参数
                   ps.setObject(i+1,params[i]);
               }
           }
            //5、执行sql语句
            int update = ps.executeUpdate();
            //6、打印返回值，update 值为受影响的行数
            if(update>0){
                System.out.println("操作成功");
                return  true;
            }
            System.out.println(update);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                ps.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println("操作失败");
        return false;
    }
   /* public static boolean executeUpdate(String sql){
        //测试获取数据库连接
        Connection connection = JdbcTest.getConnection();
        PreparedStatement preparedStatement=null;
        //3、编写sql语句
        // String sql="insert into dept (dname,loc) values('邮电部1','北京')";
        *//*String sql="delete  from dept where deptno>50";*//*
        try {
            //4、获取PreparedStatement
             preparedStatement = connection.prepareStatement(sql);
            //5、执行sql语句
            int update = preparedStatement.executeUpdate();
            //6、打印返回值，update 值为受影响的行数
            if(update>0){
                System.out.println("操作成功");
                return  true;
            }
            System.out.println(update);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        System.out.println("操作失败");
        return false;
    }*/

    public static void main(String[] args) {
        // String sql="insert into dept (dname,loc) values('邮电部1','北京')";
      /*  String sql="delete  from dept where deptno>50";*/
     /*   String sql ="update dept set dname ='xxx' where deptno=400";
        boolean update = JdbcTest.executeUpdate(sql);
        System.out.println(update);*/
   //  String  sql="insert  into tbl_class (class_name) values (?)";
     String  sql="insert  into tbl_student (id,name,phone,class_id,sex) values (?,?,?,?,?)";
     JdbcTest.executeUpdate(sql,300,"马明刚","110",3,"男");

    }
}
